package factory;

import java.util.HashMap;

import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveMapComposite;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;
import map.areaeffect.AreaEffect;


public class AEFactory implements loadsave.LoadSaveable {

    private static java.util.Map<String, AreaEffect> aeMap;
    
    /**
     * create the static map
     */
    static {
        aeMap = new java.util.HashMap<String, AreaEffect>();
    }
    
    /**
     * return an AreaEffect based on the string name
     * @param aeName
     * @return
     */
    public static AreaEffect createAreaEffect( String aeName ) {
        return aeMap.get(aeName);
    }
    
    /**
     * return an AreaEffect based on the LoadSaveObject
     * @param lso
     * @return
     */
    public static AreaEffect createAreaEffect(LoadSaveObject lso) {
    	return createAreaEffect(lso.get("name").getValue());
    }
    
    /**
     * put an AreaEffect into the map
     * @param ae
     */
    public static void addAreaEffect(AreaEffect ae) {
    	aeMap.put(ae.getName(), ae);
    }
    
    public Memento getMemento() {
    	return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("aeFactory", "AEFactory");
				LoadSaveMapComposite lsmc = new LoadSaveMapComposite("aeMap", "HashMap", "String", "AreaEffect");
				for (String s : aeMap.keySet()) {
					lsmc.add(new LoadSavePrimitive("","",s), aeMap.get(s).getMemento().getState());
				}
				toReturn.add(lsmc.getName(), lsmc);
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				AEFactory.aeMap = new HashMap<String, AreaEffect>();
				Iterator<LoadSaveObject> i = lso.get("aeMap").getIterator();
				for (i.init(); i.hasNext(); i.next()) {
					AreaEffect toAdd = new AreaEffect();
					toAdd.getMemento().restoreState(i.getCurrVal());
					AEFactory.aeMap.put(i.getCurrKey().getValue(), toAdd);
				}
			}
    		
    	};
    }
}
